/*
 * @Date: 2024-07-31 10:41:19
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2024-07-31 10:41:21
 * @Description: 
 */
import XEUtils from 'xe-utils';
import { v4 as uuidv4 } from 'uuid'

const RKUtils = {
	/** 格式化银行账号，按4位显示 */
	formatBankNo(value) {
		if (value) {
			return value
				.replace(/\s/g, '')
				.replace(/[^\d]/g, '')
				.replace(/(\d{4})(?=\d)/g, '$1 ');
		} else {
			return '';
		}
	},
	/** 格式化金额 */
	formatAmount(value, digits = 4, fillZero = true) {
		if (fillZero) {
			return XEUtils.commafy(XEUtils.toNumber(value), { digits });
		} else {
			return XEUtils.commafy(XEUtils.toNumber(value), { digits }).replace(/(?:\.0*|(\.\d+?)0+)$/, '$1');
		}
	},
	/** 格式化金额方法 */
	formatThousand: function (value, digits = 2) {
		return RKUtils.formatAmount(value, digits, true);
	},
	/** 格式化金额方法，小数点后不补零 */
	formatThousandNotFillZero: function (value, digits = 4) {
		return RKUtils.formatAmount(value, digits, false);
	},
	/** 获取GUID */
	getGuid: function () {
		return uuidv4().replace(/-/g, '').toUpperCase();
	},
	formatNumber(number) {
		if (isNaN(number)) {
			return 0;
		}
		return +number;
	},
	parseStringToObject(value) {
		if (!value) return []
		try {
			return JSON.parse(value.replace(/'/g, '"'))
		} catch (err) {
			return []
		}
	},
	parseStringToBoolean(value) {
		return value === 'true' ? true : false;
	},
	/**
	 * 获取浏览器类型
	 * 
	 * @returns {String}
	 */
	getBrowserInfo () {
		var ua = navigator.userAgent.toLocaleLowerCase();
		var browserType=null;
		if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
				browserType = "IE";
				browserVersion = ua.match(/msie ([\d.]+)/) != null ? ua.match(/msie ([\d.]+)/)[1] : ua.match(/rv:([\d.]+)/)[1];
		} else if (ua.match(/firefox/) != null) {
				browserType = "火狐";
		}else if (ua.match(/ubrowser/) != null) {
				browserType = "UC";
		}else if (ua.match(/opera/) != null) {
				browserType = "欧朋";
		} else if (ua.match(/bidubrowser/) != null) {
				browserType = "百度";
		}else if (ua.match(/metasr/) != null) {
				browserType = "搜狗";
		}else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
				browserType = "QQ";
		}else if (ua.match(/maxthon/) != null) {
				browserType = "遨游";
		}else if (ua.match(/chrome/) != null) {
				var is360 = _mime("type", "application/vnd.chromium.remoting-viewer");
				function _mime(option, value) {
						var mimeTypes = navigator.mimeTypes;
						for (var mt in mimeTypes) {
								if (mimeTypes[mt][option] == value) {
										return true;
								}
						}
						return false;
				}
				if(is360){
						browserType = '360';
				}else{
						browserType = '谷歌';
				}
		}else if (ua.match(/safari/) != null) {
				browserType = "Safari";
		}
		return browserType
	},
	/**
	 * 获取系统信息
	 */
	getOperatingSystem() {
		const platform = navigator.platform.toLowerCase();
		if (platform.includes('win')) return 'Windows';
		if (platform.includes('mac')) return 'MacOS';
		return 'Unknown';
	}
};

export default RKUtils;
